package com.yycx.module.bus.provider.handler.haiying.common;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.hy.BusBankStatement;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.client.entity.SysCompany;
import org.springframework.stereotype.Component;

/**
 * 银行流水选择
 *
 * @author zyf
 */
@Component
public class BankStatementSelectPageListHandler implements PageInterceptor<BusBankStatement> {


    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<BusBankStatement> cq, PageParams pageParams, EntityMap params) {
        cq.select(BusBankStatement.class, "*");
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String customName = cq.getParams("customName");
        String customCode = cq.getParams("customCode");
        cq.select(BusBankStatement.class, "*");
        cq.select(SysCompany.class, "companyName");
        cq.select(SysBank.class, "bankName");
        cq.eq(BusBankStatement.class, "takeBankId");
        if(FlymeUtils.isNotEmpty(customCode)){
            cq.and(e -> e.eq("bbs.customCode", customCode).or().isNull("bbs.customCode"));
        }
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "bbs.amountDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "bbs.amountDate", endDate);
        if (FlymeUtils.isNotEmpty(customName)) {
            String pinyin = PinyinUtil.getFirstLetter(customName, "").toUpperCase();
            cq.and(e -> e.like("bbs.customName", customName).or().likeRight("custom.customNamePinYin", pinyin));
        }
        //只查询未认款的
        cq.eq(true, "bbs.amountState", 0);
        //只查看当前账套
        cq.eq(true,"bbs.companyId", OpenHelper.getCompanyId());
        cq.createJoin(SysBank.class).setMainField("takeBankId");
        cq.orderByDesc("bbs.createTime");
        cq.createJoin(SysCompany.class);
        cq.createJoin(BusCustom.class);
    }

}
